<?php
/**
 * @author: Shauan Andrade < shauan@unimedteresina.com >
 * @copyright 2014 - 2014 PhpStorm
 * Date and time 14/10/14 - 17:17
 */

namespace helpers;

use core\System;

class UtilHelper{

    public $decrypt = false;

    public function cpfFormat($cpf,$format=true)
    {
        $cpf = preg_replace('/[^0-9]/','',$cpf);
        if($format) {
            if (strlen($cpf) == 11) {
                $newCpf[] = substr($cpf,0,3);
                $newCpf[] = substr($cpf,3,3);
                $newCpf[] = substr($cpf,6,3);
                $newCpf[] = substr($cpf,9,2);
                $newCpf = $newCpf[0].'.'.$newCpf[1].'.'.$newCpf[2].'-'.$newCpf[3];
            }
        }else{
            $newCpf = $cpf;
        }
        return $newCpf;
    }

    public function cnpjFormat($cnpj,$format=true)
    {
        $newCnpj = null;
        $cnpj = preg_replace('/[^0-9]/','',$cnpj);

        if($format) {
            if (strlen($cnpj) == 14) {
                $newCnpj[] = substr($cnpj,0,2);
                $newCnpj[] = substr($cnpj,2,3);
                $newCnpj[] = substr($cnpj,5,3);
                $newCnpj[] = substr($cnpj,8,4);
                $newCnpj[] = substr($cnpj,12,2);
                $newCnpj = $newCnpj[0].'.'.$newCnpj[1].'.'.$newCnpj[2].'/'.$newCnpj[3].'.'.$newCnpj[4];

            }
        }else{
            $newCnpj = $cnpj;
        }
        return $newCnpj;
    }

    public function downloadFile($pathfile,$fileName){
        $pathfile = preg_replace('/\./',DS,$pathfile);
        $file = _PUBLIC."uploads".DS.$pathfile.DS.$fileName;
        if(file_exists($file)) {

            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT');
            header('Cache-Control: private', false);
            if($this->decrypt){
                $f = explode(".", $fileName);
                $fname = $this->_getObjectSystem()->decrypt($f[0]) . "." . $f[1];
                header('Content-Disposition: attachment; filename="' .$fname. '"');
            }else{
                header('Content-Disposition: attachment; filename="'.basename($file).'"');
            }
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: ' . filesize($file)); // provide file size
            header('Content-Type:  application/force-download');
            header('Connection: close');
            ob_clean();
            flush();
            readfile($file);
        }else{
            return false;
        }
    }

    public function downloadZip($pathfile,$fileName){
        $pathfile = preg_replace('/\./',DS,$pathfile);
        $dirFile = _PUBLIC."uploads".DS.$pathfile.DS;
        $fileZip = $dirFile.$fileName.'.zip';

        if(!file_exists($fileZip)){
            $zip = new \ZipArchive();
            if($zip->open($fileZip,\ZIPARCHIVE::CREATE) !== true){
                return print "error";
            }

            $dir = dir($dirFile);
            while(($file = $dir->read()) !== false){
                $nameFile = $dirFile.$file;
                if(is_file($nameFile)){
                    if($this->decrypt){
                        $f = explode(".", $file);
                        $fname = $this->_getObjectSystem()->decrypt($f[0]) . "." . $f[1];
                        $zip->addFile($nameFile,$fname);
                    }else{
                        $zip->addFile($nameFile,basename($file));
                    }
                }

            }

            $zip->close();
        }
        chmod($fileZip,0777);
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($fileZip)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type:application/force-download ');
        header('Content-Disposition: attachment; filename="'.basename($fileZip).'"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($fileZip)); // provide file size
        header('Connection: close');
        ob_clean();
        flush();
        readfile($fileZip);
    }

    public function parseFloat($value){
        if(strpos($value,',') !== false){
            $_value = str_replace('.','',$value);
            $_value = str_replace(',','.',$_value);
        }else{
            $_value = $value;
        }
        return $_value;
    }

    private function _getObjectSystem(){
        $system = new System();
        return $system;
    }
} 